//
//  UrinalysisViewController.m
//  yilutongxing
//
//  Created by T&J on 2017/6/26.
//  Copyright © 2017年 YZE. All rights reserved.
//

#import "RenalViewController.h"
#import "SuifangSubitemViewCell.h"

@interface RenalViewController () <UITextFieldDelegate>
@property (nonatomic,strong) NSArray *rowUIArr;
@end

@implementation RenalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"肾功能";
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackGroundImage"]
                                                  forBarMetrics:UIBarMetricsDefault];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"保存"
                                                                             style:UIBarButtonItemStyleDone
                                                                            target:self
                                                                            action:@selector(saveAction)];
    UIImage *img = [[UIImage imageNamed:@"返回"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:img
                                                                            style:UIBarButtonItemStyleDone
                                                                           target:self
                                                                           action:@selector(backAction)];
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    self.tableView.allowsSelection = NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"SuifangSubitemViewCell"
                                               bundle:[NSBundle mainBundle]]
         forCellReuseIdentifier:@"cell"];
    self.rowUIArr = @[
                      @{@1:@"尿素氮", @2:@"参考值:1.7-8.3 mmol/L", @3:@"mmol/L"},
                      @{@1:@"肌酐", @2:@"参考值:53-108 μmol/L", @3:@"μmol/L"},
                      @{@1:@"尿酸", @2:@"参考值:150-420 μmol/L", @3:@"μmol/L"},
                      @{@1:@"胱抑素C", @2:@"参考值:0.63-1.25 mg/L", @3:@"mg/L"}
                      ];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 事件响应
- (void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)saveAction{
        
        if ([self.delegate respondsToSelector:@selector(backRenal:)]) {
            [self.delegate backRenal:self.renal];
        }
        
        [self backAction];
    
}

#pragma mark - Table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [SuifangSubitemViewCell staticHeight];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _rowUIArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *rowUIData = _rowUIArr[indexPath.row];
    SuifangSubitemViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[SuifangSubitemViewCell alloc]init];
    }
    cell.valTextfield.tag = indexPath.row+1;
    [cell renderUIWithText:rowUIData];
    [cell.valTextfield addTarget:self action:@selector(textFieldDidChangeValue:) forControlEvents:UIControlEventEditingChanged];
    NSString *keyname = [NSString stringWithFormat:@"rena%ld",(long)indexPath.row+1];
    if (indexPath.row==0) {
        keyname = @"renal";
    }
    cell.valTextfield.text = [self.renal valueForKey:keyname];
    return cell;
}

- (RPRenalBean *)renal{
    if (!_renal) {
        _renal = [RPRenalBean new];
    }
    return _renal;
}

#pragma mark UITextField代理
- (void)textFieldDidChangeValue:(id)sender{
    UITextField *textField = (UITextField*)sender;
    if (textField.text.length>0) {
        NSString *key = [NSString stringWithFormat:@"rena%ld", (long)textField.tag];
        if (textField.tag==1) {
            key = @"renal";
        }
        [self.renal setValue:textField.text forKey:key];
    }
}

@end
